<script setup lang="tsx">
import { reactive, ref } from 'vue';
import { UUID } from 'uuidjs';
import { getTableData } from '@/service/api/modules/collection-management/equipment-alarm-info';
import Search from './modules/search.vue';

const searchParams = ref({
  equipmentId: null,
  alarmText: null,
  time: null
});
const pagination = reactive({
  current: 1,
  pageSize: 200,
  showSizeChanger: true,
  pageSizeOptions: ['50', '100', '200', '300', '500'],
  showQuickJumper: true,
  showTotal: (total: number, range: number[]) => {
    return `共${total}条`;
  },
  total: 0,
  onChange: async (current: number, size: number) => {
    pagination.current = current;
    pagination.pageSize = size;
    getData();
  }
});

const tableData = ref([]);
const loading = ref(false);
const columnChecks = ref<any[]>([
  {
    key: 'index',
    dataIndex: 'index',
    title: '序号',
    width: 70,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'mainEqpId',
    dataIndex: 'mainEqpId',
    title: '线体代码',
    width: 140,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'mainEqpName',
    dataIndex: 'mainEqpName',
    title: '线体名称',
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'subEqpId',
    dataIndex: 'subEqpId',
    title: '设备',
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'subEqpName',
    dataIndex: 'subEqpName',
    title: '设备名称',
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'dateTime',
    dataIndex: 'dateTime',
    title: '建立时间',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'alarmId',
    dataIndex: 'alarmId',
    title: '警报代码',
    align: 'center',
    checked: true,
    width: 100,
    ellipsis: true
  },
  {
    key: 'alarmCategory',
    dataIndex: 'alarmCategory',
    title: '警报类型',
    align: 'center',
    width: 100,
    checked: true,
    ellipsis: true
  },
  {
    key: 'alarmText',
    dataIndex: 'alarmText',
    title: '警报内容',
    align: 'center',
    width: 200,
    checked: true,
    ellipsis: true
  },
  {
    key: 'startTime',
    dataIndex: 'startTime',
    title: '开始时间',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'endTime',
    dataIndex: 'endTime',
    title: '结束时间',
    align: 'center',
    checked: true,
    ellipsis: true
  }
]);
function reset() {
  search();
}

function search() {
  pagination.current = 1;
  getData();
}

function getData() {
  const params: any = {
    pageNum: pagination.current,
    pageSize: pagination.pageSize,
    equipmentId: searchParams.value.equipmentId,
    alarmText: searchParams.value.alarmText
  };
  if (searchParams.value.time?.[0]) {
    params.beginTime = searchParams.value.time[0];
    params.endTime = searchParams.value.time[1];
  }
  loading.value = true;
  getTableData(params)
    .then(({ data, error }) => {
      if (!error) {
        data.list.forEach((item: any, index: number) => {
          item.index = index + 1;
          item.uid = UUID.genV4().hexNoDelim;
        });
        pagination.total = data.total;
        tableData.value = data.list;
      }
    })
    .finally(() => (loading.value = false));
}

getData();

const currentRowId = ref(null);
const customRow = record => {
  return {
    style: {
      // 设置字体颜色
      // 'color': record.id === this.leftAlignId ? '#fff' : '',
      //  设置行背景色
      // 'background-color': record.uid === currentRowId.value ? 'rgba(86, 205, 243, 0.3)' : ''
      // 设置字体类型
      // 'font-family': 'Microsoft YaHei',
      // // 下划线
      // 'text-decoration': 'underline',
      // // 字体样式-斜体
      // 'font-style': 'italic',
      // // 字体加粗
      // 'font-weight': 'bold'
      background: record.uid === currentRowId.value ? '#BAEAFA' : ''
    },
    class: record.uid === currentRowId.value ? 'jl-table-row-selected' : '',
    onClick: () => {
      currentRowId.value = record.uid;
    }
  };
};
</script>

<template>
  <div class="jl-main-page one-code-ascend min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <Search v-model:model="searchParams" @reset="reset" @search="search" />
    <ACard
      :bordered="false"
      :body-style="{ flex: 1, overflow: 'hidden' }"
      class="table-card flex-col-stretch sm:flex-1-hidden card-wrapper"
    >
      <template #extra>
        <TableColumnSetting v-model:columns="columnChecks" />
      </template>
      <STable
        :columns="columnChecks"
        :data-source="tableData"
        bordered
        size="small"
        :loading="loading"
        row-key="uid"
        :pagination="pagination"
        :custom-row="customRow"
        :scroll="{ x: 1680, y: 'auto' }"
        class="h-full"
        :row-height="23"
      />
    </ACard>
  </div>
</template>

<style></style>
